<?php
/**
 * 绵阳妇幼中间件
 *
 *
 * @author wangwh
 */

namespace Pzhrs\Controller;

class PzhqqControlController
{
    // SoapClient 对象
    private $soap = FALSE;

    // 记录是否为正式环境
    private $isFormal = FALSE;

    //是否开启缓存
    private static $CACHE = false;

    // 医疗机构科室列表缓存建
    private static $CACHE_INSTITUTION_CLASS_LIST = 'CACHE_INSTITUTION_CLASS_LIST';


    static $URL_API = 'http://220.166.60.252:8083/wxjk.dll/wsdl/Iwxjk';//http://192.168.203.55:8078/HisService.asmx?WSDL
    static $back_URL_API = "http://localhost:8081/";

    //医院id
    private $hospitalId;

    //自助机id
    private $userId;

    /**
     * 构造函数
     */
    function __construct()
    {
        header('Content-Type: text/html; charset=utf-8');
        $this->hospitalId = 75;
        if (!method_exists($this, ACTION_NAME)) {
            $function = ACTION_NAME;
            A('Receive')->$function();
        }
    }

    public function xml_to_array($list){
        $ob= simplexml_load_string($list);
        $json  = json_encode($ob);
        $configData = json_decode($json, true);
        return $configData;
    }

    public $hospitalKey = array(
        "75" => "7daf7603429012aeae417fa9beed9c75",
    );
    //正式地址
    //	static $URL_API = "http://125.64.9.81:8088/rmc/mobile/web";
    //	static $version = "2.2.7";
    //	private $token = "6a2967e64a994e54b533aa7942328179";
    //正式地址


    // 	static $URL_API = "http://125.64.9.81:8088/rmc/mobile/web";
    /**
     * 请求His API
     *
     *   使用SoapClient请求HIS webservice接口
     *
     * @param String $Flag 类型
     * @param String $Content xml请求内容
     * @param String $Result_K 返回xml中的跟节点key
     *
     * @return array
     */


    private function soapGetDate($Flag, $Content)
    {
//        $this->isFormal = ($_SERVER['SERVER_NAME']!="localhost" && "histest.mobimedical.cn"!=$_SERVER['SERVER_NAME']);
        $this->isFormal = false;

        if (!$this->soap) {
            $this->soap = new \SoapClient(self::$URL_API, array("connection_timeout" => 15));
        }
        try {

            /* 正式环境 */
            if ($this->isFormal) {
                //专线接口,走平台
                try {
                    $result = $this->soap->Weixin($Content);
                } catch (\Exception $e) {
                    $fileContent = $Content . "\n" . $e->getMessage();
                    $dir = "soapGetDate/";
                    \Org\Util\File::createDir($dir);
                    file_put_contents($dir . $Flag . "_error.txt", $fileContent);
                    return array('ResultCode' => -1, "ErrorMsg" => "系统异常，请稍后重试21！");
                }
                $Result_K = "Weixin" . Result;

                /* 测试环境 */
            } else {
                $result = $this->soap->$Flag( $Content);
                /* 记录请求日志   */
                $fileContent = $Content . "\r" . var_export($result, true);
                $dir = "soapGetDate/" . date("Y-m-d") . "/" . $Flag . "/";
                $filename = date("H_i_s") . "_" . rand(10000, 99999);
                writeLog($dir, $filename, $fileContent);

                return json_decode(json_encode(json_decode(json_encode((array)simplexml_load_string(substr($result, 54)), true))), true);
            }


        } catch (\Exception $e) {
            /* 记录请求异常日志  */
            $fileContent = $Content . "\n" . $e->getMessage();
            $dir = "soapGetDate/" . date("Y-m-d") . "/" . $Flag . "/";
            $filename = date("H_i_s") . "_error";
            writeLog($dir, $filename, $fileContent);
            return array('ResultCode' => -1, "ErrorMsg" => "系统异常，请稍后重试22！");
            exit;
        }
    }


    function Post($url, $method, $post = null)
    {
        if (is_array($post)) {
            ksort($post);
            $content = http_build_query($post);
            $content_length = strlen($content);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' =>
                        "Content-type: application/x-www-form-urlencoded; charset=utf-8\r\n" .
                        "Content-length: $content_length\r\n",
                    'content' => $content
                )
            );
            $res = file_get_contents($url, false, stream_context_create($options));
            $fileContent = var_export($post, true) . "\r" . var_export($res, true) . "\r" . var_export(json_decode($res, true), true);
            $dir = "soapGetDateXt/" . date("Y-m-d") . "/" . $method . "/";
            $filename = date("H_i_s") . "_" . rand(10000, 99999);
            writeLog($dir, $filename, $fileContent);
            return $res;
        }
    }

    /**
     * 中间件签名验证
     * @param array $params
     * @return string
     */
    function middelEnCodeSign($params)
    {
        ksort($params);
        $preString = "";
        $time = $params['time'];
        $hospitalId = $params['hospitalId'];
        foreach ($params as $k => $v) {
            if ($k == 'time' || $k == 'sign' || !$v)
                continue;
            $preString .= $k . "=" . $v;
        }

        $keyString = $this->hospitalKey[$hospitalId];
        //加密
        return sha1($preString . $keyString . $time);
    }



    /**
     * 统一调接口方法 
     * $xml       接口传递的参数
     * $funNo     接口的方法名
     * $client    接口需要调用的webservice对象 
     * */
    function Get_service_api($xml,$funNo,$client){
        $mmkj='http://yinhai.com';
        $code='wx20170921'; //授权码
        //$apiUrl='http://pzh.91cure.com/index.php?g=Home&m=Test&a=pzh_sign';
        $apiUrl='http://pzhrs.91cure.com/index.php?g=Home&m=Test&a=pzh_sign';
        $params1 = array();
        $params1['content']=$xml;
        $xmlsing=$this->PostData_jm($apiUrl,$params1);
        //echo strlen($xmlsing);
        //echo "<br>";
        //echo strlen($xmlsing1);
        //echo "<br>";
        //dump($xmlsing1);die();
        $auth_header = array( "jybh" =>$funNo ,"systemKey" =>$code ,"source" =>$xml,"signature" =>$xmlsing  );
        $authvalues = new \SoapVar($auth_header, SOAP_ENC_OBJECT,"system",$mmkj);  
        $u = new \SoapHeader($mmkj, 'system', $authvalues);
        $client->__setSoapHeaders($u);
        $params['inputxml']=$xml;
        $suc=$client->callBusiness($params);
        $data=$suc->return;
        $tesres = $this->xml_to_array($data);
        return  $tesres;
    }
    /**
     * 数字签名
     * $url         签名地址
     * $post        签名参数
     * */
    function PostData_jm($url, $post = null) {
        if (is_array($post)) {
            ksort($post);
            $content = http_build_query($post);
            $content_length = strlen($content);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' =>
                    "Content-type: application/x-www-form-urlencoded\r\n"."Content-length: $content_length\r\n",
                    'content' => $content
                )
            );
            $now = time();
            $res = file_get_contents($url, false, stream_context_create($options));
            return substr(urlencode($res),9);//
            //return $res;
        }
    }

    /**
     * 社保卡绑定与解绑
     * @param 
     */
    function GetTiedCard_Reg(){
        //$identyId='513023198605070153';   //身份证号码
        //$name='陈柯江';       //姓名
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8';      //第三方ID ‘tcyl_’加微信openid
        //$phone='13550908933';          //电话2223988  13550908933
        //$Card_no='D04016899';        //社保卡卡号 D13825348 4000611622  D04016899
        //$Trading_channels='01';   //交易渠道 渠道目前只有微信,请默认写01
        //$Transaction_Type='1';   //交易类型 1-绑定,2-解绑,3-修改
        $tcylkey = C('TCYL_KEY');
        $identyId  = $_POST['identyId'];
        $name  = $_POST['name'];
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        $phone  = $_POST['phone'];
        $Card_no  = $_POST['Card_no'];
        $Trading_channels  = $_POST['Trading_channels'];
        $Transaction_Type  = $_POST['Transaction_Type'];
        $funNo='WX_010';    //交易编号
        //dump(C('URL_API'));die();
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901><aac002>".$identyId."</aac002><aac003>".$name."</aac003><aae005>".$phone."</aae005><akc020>".$Card_no."</akc020><yad961>".$Trading_channels."</yad961><yad941>".$Transaction_Type."</yad941></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 养老保险缴费信息查询
     * @param 
     */
    function GetPensionPayment_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_001';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        //dump($xml);
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 养老保险退休信息查询
     * @param 
     */
    function GetPensionRetirement_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_002';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        //dump($xml);
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 医疗保险缴费信息查询
     * @param 
     */
    function GetMedicalInsurance_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_003';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 职工医疗保险缴费信息查询
     * @param 
     */
    function GetMedicalInsuranceZG_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_003_ZG';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 居民医疗保险缴费信息查询
     * @param 
     */
    function GetMedicalInsuranceJM_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_003_JM';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 工伤保险缴费信息查询
     * @param 
     */
    function GetInjuryInsurance_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_004';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 生育保险缴费信息查询
     * @param 
     */
    function GetMaternityInsurance_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_005';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 失业保险缴费信息查询
     * @param 
     */
    function GetUnemploymentInsurance_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_006';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 失业保险待遇信息查询
     * @param 
     */
    function GetUnemploymentTreatment_Inquire(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_007';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 医保消费明细查询
     * @param 
     */
    function GetMedicalAccountExpenditure_Details(){
        $tcylkey = C('TCYL_KEY');
        $wx_openid  = $tcylkey.$_POST['wx_openid'];
        $StartData = $_POST['StartData'];    //开始行
        $EndData = $_POST['EndData'];      //结束行
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        $funNo='WX_009';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901>><aae030>".$StartData."</aae030><aae031>".$EndData."</aae031></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 参保人缴费明细信息查询
     * @param 
     */
    function GetPayment_Details(){
        $tcylkey = C('TCYL_KEY');
        //$wx_openid  = $tcylkey.'oeTsDv8zEhse5Hs_isEog1ncAlQ8';
        $wx_openid = $tcylkey.$_POST['wx_openid'];
        $Insurance_Type = $_POST['Insurance_Type'];    //险种类型 1
        $StartIssue = $_POST['StartIssue'];  //开始期号
        $EndIssue = $_POST['EndIssue'];    //结束期号  结束期号-开始期号<=12
        $funNo='WX_011';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901><aae140>".$Insurance_Type."</aae140><aae030>".$StartIssue."</aae030><aae031>".$EndIssue."</aae031></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        //dump($returnArr);
        echo json_encode($result_list);
    }

    /**
     * 医保查询信息(注册之前查询社保卡信息)
     * @param 
     */
    function GetShebaoChaxun(){
        //$user_card_type = '1';    //证件类型
        //$user_card_no = '513023198605070153';      //证件号码
        //$user_name = '陈柯江';         //姓名
        //$mi_card_no = 'D04016899';        //医保卡号
        
        $user_card_type  = $_POST['user_card_type'];
        $user_card_no  = $_POST['user_card_no'];
        $user_name  = $_POST['user_name'];
        $mi_card_no  = $_POST['mi_card_no'];
        $channel = '01';       //渠道 01 微信
        $bank_card_no = '';      //银行卡号
        $extend_params = '';     //扩展参数
        $funNo='WX_013';    //交易编号
        $xml="<input><inputxml><inputxml><aac002>".$user_card_no."</aac002><aac009>".$user_card_type."</aac009><aac003>".$user_name."</aac003><akc020>".$mi_card_no."</akc020><yad961>".$channel."</yad961><aae010>".$bank_card_no."</aae010><aae013>".$extend_params."</aae013></inputxml></inputxml></input>";
        
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    /**
     * 职工医保个人账户收支查询
     * @param 
     */
    function GetPersonalIncome_Expenditure(){
        $tcylkey = C('TCYL_KEY');
        //$wx_openid  = 'tcyl_oeTsDvx_c9sJhQ3XrqxBeVsfDJxY';
        //$IncomeExpenditure_Type = '1';    //收支类型  1收入,2支出
        //$StartData = '1';  //开始行
        //$EndData = '10';    //结束行

        $wx_openid = $tcylkey.$_POST['wx_openid'];
        $IncomeExpenditure_Type = $_POST['IncomeExpenditure_Type'];
        $StartData = $_POST['StartData'];
        $EndData = $_POST['EndData'];
        $funNo='WX_015';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901><yad904>".$IncomeExpenditure_Type."</yad904><aae030>".$StartData."</aae030><aae031>".$EndData."</aae031></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }
    /**
     * 制卡查询
     * @param 
     */
    function GetCard_Inquiries(){
        $tcylkey = C('TCYL_KEY');
        //$wx_openid='tcyl_oeTsDv8zEhse5Hs_isEog1ncAlQ8'; //第三方ID
        //$identyId='510402198306076317';    //社会保障号(身份证号码)
        //$name='张帆';     //姓名

        $wx_openid = $tcylkey.$_POST['wx_openid'];
        $identyId = $_POST['identyId'];
        $name = $_POST['name'];
        $funNo='WZ2000';    //交易编号
        $xml="<input><inputxml><inputxml><yad901>".$wx_openid."</yad901><aac002>".$identyId."</aac002><aac003>".$name."</aac003><fwid>".$funNo."</fwid></inputxml></inputxml></input>";
        $client=new \SoapClient(C('URL_API'));
        $result_list=$this->Get_service_api($xml,$funNo,$client);
        echo json_encode($result_list);
    }

    function qq(){
        echo 666;
    }
}
?>